package com.songtech.uitest.module.deliver;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.module.screen.ScreenTower;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ThirdResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class DeliverTower {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private ThirdResource thirdResource;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private StepParser stepParser;

    @Autowired
    private AlertHandler alertHandler;

    private Logger logger = LoggerFactory.getLogger(DeliverTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public void deliverTower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);

        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "交付确认数据模拟", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写交付详情表单");

        //填表单
        driver.findElement(By.id("towerOrderCode")).sendKeys(indent.getTowerId());
        driver.findElement(By.id("requestTime")).click();
        driver.findElement(By.id("proName")).clear();
        driver.findElement(By.id("proName")).sendKeys(indentResource.getDeliverProName());
        driver.findElement(By.id("province")).clear();
        driver.findElement(By.id("province")).sendKeys(indentResource.getDeliverProvince());
        driver.findElement(By.id("region")).clear();
        driver.findElement(By.id("region")).sendKeys(indentResource.getDeliverRegion());
        driver.findElement(By.id("county")).clear();
        driver.findElement(By.id("county")).sendKeys(indentResource.getDeliverCountry());
        driver.findElement(By.id("workstarttime")).click();//实际开工日期
        driver.findElement(By.id("workendtime")).click();//实际交工日期
        driver.findElement(By.id("realityDelivertime")).click();//实际交付日期
        driver.findElement(By.id("checktime")).click();//验收时间
        //移动是否使用站址
        Select existCm = new Select(driver.findElement(By.id("existCm")));
        existCm.selectByVisibleText(indentResource.getDeliverExistCm());
        //站址涉及客户数
        driver.findElement(By.id("siteclientCount")).clear();
        driver.findElement(By.id("siteclientCount")).sendKeys(indentResource.getDeliverSiteClientCount());
        //建设方式
        String buildTypeStr = indentResource.getDeliverBuildType();
        String buildTypeNum = "";
        if(buildTypeStr.equals("新建")){ buildTypeNum = "01"; }
        if(buildTypeStr.equals("改造")){ buildTypeNum = "03"; }
        if(buildTypeStr.equals("存量直接满足")){buildTypeNum = "05"; }
        driver.findElement(By.id("buildType")).clear();
        driver.findElement(By.id("buildType")).sendKeys(buildTypeNum);
        //经度纬度
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(indentResource.getDeliverLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(indentResource.getDeliverLatitude());


        driver.findElement(By.id("saveSet")).click();


        //完成后，设置当前站址类型
        indent.setCurrentBuildType(indentResource.getDeliverResourceType());
        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "模拟交付表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(6000);

//        AlertHandler.checkAndGo(driver);  //可能是页面长了，有问题

        Thread.sleep(3000);


        tabHandler.closeAllTabs(driver);


    }


    public void deliver3Tower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);

        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "交付编制", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        //查询此单
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);

        //拖动滚动条到最右方
        String js = "$(\"div.fixed-table-body\").scrollLeft(document.body.scrollWidth)";
        ((JavascriptExecutor) driver).executeScript(js);
        Thread.sleep(1000);
        driver.findElement(By.xpath("//button[text()='编制交付验收']")).click();

        //转到三方交付编制详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方交付编制详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        Thread.sleep(1000);
        logger.info(space.getSpace() + "开始填写三方交付验收表单");

        //填表单
        //重置
        driver.findElement(By.xpath("//button[text()='重置']")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//button[text()='确定']")).click();
        Thread.sleep(1000);

        //站址名称
        driver.findElement(By.id("siteAddressName")).clear();
        driver.findElement(By.id("siteAddressName")).sendKeys(thirdResource.getDeliverSiteAddressName());
        //站址编码
        driver.findElement(By.id("siteAddressCode")).sendKeys("aabbcc");
        //建设方式，存量直接满足，新建，改造
        Select buildType = new Select(driver.findElement(By.id("buildType")));
        buildType.selectByVisibleText(thirdResource.getDeliverBuildType());
        //经度纬度
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(thirdResource.getDeliverLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(thirdResource.getDeliverLatitude());
        //实际开工日期，交工日期
        driver.findElement(By.id("workstarttime")).click();
        Thread.sleep(500);
        driver.findElement(By.id("workendtime")).click();
        Thread.sleep(500);
        //移动是否已经使用站址
        Select existCm = new Select(driver.findElement(By.id("existCm")));
        existCm.selectByVisibleText(thirdResource.getDeliverExistCm());
        //站址涉及客户数:1,2,3
        Select siteclientCount = new Select(driver.findElement(By.id("siteclientCount")));
        siteclientCount.selectByVisibleText(thirdResource.getDeliverSiteClientCount());
        //验收时间
        driver.findElement(By.id("checktime")).click();
        Thread.sleep(500);

        driver.findElement(By.xpath("//button[text()='确认并提交']")).click();
        Thread.sleep(6000);

        //完成后，设置当前站址类型
        indent.setCurrentBuildType(thirdResource.getDeliverResourceType());
        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "三方交付表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(6000);

        alertHandler.checkAndGo(driver);

        Thread.sleep(3000);


        tabHandler.closeAllTabs(driver);


    }

}
